<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

/**
 * 发票模型
 */
class OrderInvoice extends Model
{
    use SoftDelete;

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected $deleteTime = 'deletetime';
    // 追加属性
    protected $append = [

    ];


    public function getImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        if(!$value){ return  [];}
        $value = explode(',',$value);
        foreach ($value as $k=>&$v){
            $v=get_app_image_url($v);
        }
        return $value;
    }
    /**
     * 新增发票信息
     * */
    public static function OrderInvoiceAdd($UserInvoice,$order_id,$type,$price=0){
        $data = UserInvoice::where(['id'=>$UserInvoice])->find()->toArray();
        if(!empty($data)){
            unset($data['id']);
            unset($data['createtime']);
            unset($data['updatetime']);
            unset($data['apply_status']);
            unset($data['status']);
            $data['order_id'] = $order_id;
            $data['types']    = $type;
            $data['price']    = $price;
            return self::create($data);
        }
        return  false;

    }
    /*
     * 通过订单id查询数据
     * */
    public  static  function getInfo($order_id){
        $where['order_id']=$order_id;
        $info = self::where($where)
            ->find();
        $info['createtime'] =datetime($info['createtime']);
        return $info;
    }
}
